
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>5.1  String类 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="5.2String类的常用方法.html" />
    
    
    <link rel="prev" href="d5z.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    目录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../d1z/d1z.html">
            
                <a href="../d1z/d1z.html">
            
                    
                    第一章    初识java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../d1z/1.1计算机语言.html">
            
                <a href="../d1z/1.1计算机语言.html">
            
                    
                    1.1  计算机语言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../d1z/1.2java发展史.html">
            
                <a href="../d1z/1.2java发展史.html">
            
                    
                    1.2  java发展史
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../d1z/1.3Java的特点.html">
            
                <a href="../d1z/1.3Java的特点.html">
            
                    
                    1.3  Java的特点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../d1z/1.4Java程序工作原理.html">
            
                <a href="../d1z/1.4Java程序工作原理.html">
            
                    
                    1.4  Java程序工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../d1z/1.5JavaSE的结构.html">
            
                <a href="../d1z/1.5JavaSE的结构.html">
            
                    
                    1.5  Java SE的结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../d1z/1.6Java开发环境搭建.html">
            
                <a href="../d1z/1.6Java开发环境搭建.html">
            
                    
                    1.6  Java开发环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../d1z/1.7第一个Java程序.html">
            
                <a href="../d1z/1.7第一个Java程序.html">
            
                    
                    1.7  第一个Java程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../d1z/1.8本章练习.html">
            
                <a href="../d1z/1.8本章练习.html">
            
                    
                    1.8  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../d2z/d2z.html">
            
                <a href="../d2z/d2z.html">
            
                    
                    第二章    数据类型和运算符
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../d2z/2.1标识符和关键字.html">
            
                <a href="../d2z/2.1标识符和关键字.html">
            
                    
                    2.1  标识符和关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../d2z/2.2变量和常量.html">
            
                <a href="../d2z/2.2变量和常量.html">
            
                    
                    2.2  变量和常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../d2z/2.3Java数据类型.html">
            
                <a href="../d2z/2.3Java数据类型.html">
            
                    
                    2.3  Java数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../d2z/2.4成员变量和局部变量.html">
            
                <a href="../d2z/2.4成员变量和局部变量.html">
            
                    
                    2.4  成员变量和局部变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../d2z/2.5Java运算符.html">
            
                <a href="../d2z/2.5Java运算符.html">
            
                    
                    2.5  Java运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../d2z/2.6Java表达式.html">
            
                <a href="../d2z/2.6Java表达式.html">
            
                    
                    2.6  Java表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../d2z/2.7本章练习.html">
            
                <a href="../d2z/2.7本章练习.html">
            
                    
                    2.7  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../d3z/d3z.html">
            
                <a href="../d3z/d3z.html">
            
                    
                    第三章    流程控制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../d3z/3.1if语句.html">
            
                <a href="../d3z/3.1if语句.html">
            
                    
                    3.1  if语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../d3z/3.2switch语句.html">
            
                <a href="../d3z/3.2switch语句.html">
            
                    
                    3.2  switch语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../d3z/3.3循环语句.html">
            
                <a href="../d3z/3.3循环语句.html">
            
                    
                    3.3  循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../d3z/3.4本章练习.html">
            
                <a href="../d3z/3.4本章练习.html">
            
                    
                    3.4  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../d4z/d4z.html">
            
                <a href="../d4z/d4z.html">
            
                    
                    第四章    方法与数组
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../d4z/4.1方法.html">
            
                <a href="../d4z/4.1方法.html">
            
                    
                    4.1  方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../d4z/4.2数组.html">
            
                <a href="../d4z/4.2数组.html">
            
                    
                    4.2  数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../d4z/4.3二维数组.html">
            
                <a href="../d4z/4.3二维数组.html">
            
                    
                    4.3  二维数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../d4z/4.4本章练习.html">
            
                <a href="../d4z/4.4本章练习.html">
            
                    
                    4.4  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="d5z.html">
            
                <a href="d5z.html">
            
                    
                    第五章    String字符串
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.6.1" data-path="5.1String类.html">
            
                <a href="5.1String类.html">
            
                    
                    5.1  String类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="5.2String类的常用方法.html">
            
                <a href="5.2String类的常用方法.html">
            
                    
                    5.2  String类的常用方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="5.3JavaAPI文档的使用.html">
            
                <a href="5.3JavaAPI文档的使用.html">
            
                    
                    5.3  Java API文档的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="5.4StringBuffer类.html">
            
                <a href="5.4StringBuffer类.html">
            
                    
                    5.4  StringBuffer类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="5.5本章练习.html">
            
                <a href="5.5本章练习.html">
            
                    
                    5.5  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../d6z/d6z.html">
            
                <a href="../d6z/d6z.html">
            
                    
                    第六章    类和对象
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../d6z/6.1类和对象概述.html">
            
                <a href="../d6z/6.1类和对象概述.html">
            
                    
                    6.1  类和对象概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../d6z/6.2Java类.html">
            
                <a href="../d6z/6.2Java类.html">
            
                    
                    6.2  Java类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../d6z/6.3构造方法.html">
            
                <a href="../d6z/6.3构造方法.html">
            
                    
                    6.3  构造方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../d6z/6.4对象初始化过程.html">
            
                <a href="../d6z/6.4对象初始化过程.html">
            
                    
                    6.4  对象初始化过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../d6z/6.5重载.html">
            
                <a href="../d6z/6.5重载.html">
            
                    
                    6.5  重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../d6z/6.6本章练习.html">
            
                <a href="../d6z/6.6本章练习.html">
            
                    
                    6.6  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../d7z/d7z.html">
            
                <a href="../d7z/d7z.html">
            
                    
                    第七章    包和访问控制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../d7z/7.1Java包.html">
            
                <a href="../d7z/7.1Java包.html">
            
                    
                    7.1  Java包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../d7z/7.2引用包.html">
            
                <a href="../d7z/7.2引用包.html">
            
                    
                    7.2  引用包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../d7z/7.3访问权限.html">
            
                <a href="../d7z/7.3访问权限.html">
            
                    
                    7.3  访问权限
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../d7z/7.4static关键字.html">
            
                <a href="../d7z/7.4static关键字.html">
            
                    
                    7.4  static关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../d7z/7.5本章练习.html">
            
                <a href="../d7z/7.5本章练习.html">
            
                    
                    7.5  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../d8z/d8z.html">
            
                <a href="../d8z/d8z.html">
            
                    
                    第八章    继承和多态
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../d8z/8.1抽象和封装.html">
            
                <a href="../d8z/8.1抽象和封装.html">
            
                    
                    8.1  抽象和封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../d8z/8.2继承.html">
            
                <a href="../d8z/8.2继承.html">
            
                    
                    8.2  继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../d8z/8.3多态.html">
            
                <a href="../d8z/8.3多态.html">
            
                    
                    8.3  多态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../d8z/8.4本章练习.html">
            
                <a href="../d8z/8.4本章练习.html">
            
                    
                    8.4  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../d9z/d9z.html">
            
                <a href="../d9z/d9z.html">
            
                    
                    第九章    抽象类和接口
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../d9z/9.1抽象类.html">
            
                <a href="../d9z/9.1抽象类.html">
            
                    
                    9.1  抽象类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../d9z/9.2接口.html">
            
                <a href="../d9z/9.2接口.html">
            
                    
                    9.2  接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../d9z/9.3本章练习.html">
            
                <a href="../d9z/9.3本章练习.html">
            
                    
                    9.3  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../d10z/d10z.html">
            
                <a href="../d10z/d10z.html">
            
                    
                    第十章    排序算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../d10z/10.1排序算法.html">
            
                <a href="../d10z/10.1排序算法.html">
            
                    
                    10.1  排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../d10z/10.2本章练习.html">
            
                <a href="../d10z/10.2本章练习.html">
            
                    
                    10.2  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >5.1  String类</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="51--string&#x7C7B;">5.1  String&#x7C7B;</h2>
<p>&#x2003;&#x2003;String&#x7C7B;&#x8868;&#x793A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;Java &#x7A0B;&#x5E8F;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x4E32;&#xFF08;&#x4F8B;&#x5982;&#x201C;&#x96F7;&#x9759;&#x201D;&#xFF09;&#x90FD;&#x4F5C;&#x4E3A;&#x6B64;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;&#x3002;String&#x7C7B;&#x4E0D;&#x662F;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#x3002;&#x56E0;&#x4E3A;&#x5BF9;&#x8C61;&#x7684;&#x521D;&#x59CB;&#x5316;&#x9ED8;&#x8BA4;&#x503C;&#x662F;null&#xFF0C;&#x6240;&#x4EE5;String&#x7C7B;&#x5BF9;&#x8C61;&#x7684;&#x521D;&#x59CB;&#x5316;&#x9ED8;&#x8BA4;&#x503C;&#x4E5F;&#x662F;null&#x3002;String&#x662F;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5177;&#x6709;&#x5176;&#x4ED6;&#x5BF9;&#x8C61;&#x6CA1;&#x6709;&#x7684;&#x4E00;&#x4E9B;&#x7279;&#x6027;&#x3002;  </p>
<p>&#x2003;&#x2003;String&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5E38;&#x91CF;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x503C;&#x5728;&#x521B;&#x5EFA;&#x4E4B;&#x540E;&#x4E0D;&#x80FD;&#x66F4;&#x6539;&#x3002;  </p>
<p>&#x2003;&#x2003;String&#x7C7B;&#x662F;&#x6700;&#x7EC8;&#x7C7B;&#xFF0C;&#x4E0D;&#x80FD;&#x88AB;&#x7EE7;&#x627F;&#x3002;  </p>
<h3 id="511--string&#x7C7B;&#x7684;&#x6982;&#x5FF5;">5.1.1  String&#x7C7B;&#x7684;&#x6982;&#x5FF5;</h3>
<p>&#x2003;&#x2003;&#x5982;&#x4F55;&#x4F7F;&#x7528;String&#x7C7B;&#x64CD;&#x4F5C;&#x5B57;&#x7B26;&#x4E32;&#x5462;&#xFF1F;&#x9996;&#x5148;&#x8981;&#x5B9A;&#x4E49;&#x5E76;&#x521D;&#x59CB;&#x5316;&#x5B57;&#x7B26;&#x4E32;&#x3002;String&#x7C7B;&#x5305;&#x62EC;&#x4EE5;&#x4E0B;&#x5E38;&#x7528;&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x3002;  </p>
<ul>
<li><p>String(String s)&#xFF1A;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;&#x65B0;&#x521B;&#x5EFA;&#x7684;String&#x5BF9;&#x8C61;&#xFF0C;&#x4F7F;&#x5176;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x4E0E;&#x53C2;&#x6570;&#x76F8;&#x540C;&#x7684;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x3002;</p>
</li>
<li><p>String(char[] value)&#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;String&#x5BF9;&#x8C61;&#xFF0C;&#x4F7F;&#x5176;&#x8868;&#x793A;&#x5B57;&#x7B26;&#x6570;&#x7EC4;&#x53C2;&#x6570;&#x4E2D;&#x5F53;&#x524D;&#x5305;&#x542B;&#x7684;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x3002;</p>
</li>
<li><p>String(char[] value, int offset, int count)&#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;String&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x5305;&#x542B;&#x53D6;&#x81EA;&#x5B57;&#x7B26;&#x6570;&#x7EC4;&#x53C2;&#x6570;&#x7684;&#x4E00;&#x4E2A;&#x5B50;&#x6570;&#x7EC4;&#x7684;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x3002;offset&#x53C2;&#x6570;&#x662F;&#x5B50;&#x6570;&#x7EC4;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x7684;&#x7D22;&#x5F15;&#xFF08;&#x4ECE;0&#x5F00;&#x59CB;&#x5EFA;&#x7ACB;&#x7D22;&#x5F15;&#xFF09;&#xFF0C;count &#x53C2;&#x6570;&#x6307;&#x5B9A;&#x5B50;&#x6570;&#x7EC4;&#x7684;&#x957F;&#x5EA6;&#x3002;</p>
</li>
</ul>
<p>&#x2003;&#x2003;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code>String stuName1 = new String(&quot;&#x738B;&#x4E91;&quot;);

char[] charArray = {&apos;&#x5218;&apos;,&apos;&#x9759;&apos;,&apos;&#x6D9B;&apos;};

String stuName2 = new String(charArray);

String stuName3 = new String(charArray,1,2);//&#x4ECE;&apos;&#x9759;&apos;&#x5B57;&#x5F00;&#x59CB;&#xFF0C;&#x622A;&#x53D6;2&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x7ED3;&#x679C;&#x662F;&#x201C;&#x9759;&#x6D9B;&#x201D;
</code></pre><p>&#x2003;&#x2003;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x6700;&#x5E38;&#x4F7F;&#x7528;&#x7684;&#x521B;&#x5EFA;String&#x7C7B;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x65B9;&#x6CD5;&#x5982;&#x4E0B;&#x3002;  </p>
<pre><code>String stuName1 = &quot;&#x738B;&#x4E91;&quot;;
</code></pre><p>&#x2003;&#x2003;&#x5728;&#x5B9E;&#x9645;&#x7F16;&#x7A0B;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5E38;&#x5E38;&#x6709;&#x8FD9;&#x6837;&#x7684;&#x9700;&#x6C42;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x540E;&#x9762;&#x589E;&#x52A0;&#x4E00;&#x4E9B;&#x5185;&#x5BB9;&#xFF0C;&#x4F8B;&#x5982;&#x9700;&#x8981;&#x5728;stuName1&#x540E;&#x9762;&#x589E;&#x52A0;&#x5B57;&#x7B26;&#x4E32;&#x201C;&#x540C;&#x5B66;&#x201D;&#x3002;&#x67E5;&#x8BE2;&#x76F8;&#x5173;&#x8D44;&#x6599;&#xFF0C;&#x77E5;&#x9053;String&#x7C7B;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;concat(String str)&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;String&#x7C7B;&#x5B57;&#x7B26;&#x4E32;&#x540E;&#x9762;&#x589E;&#x52A0;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x6709;&#x5982;&#x4E0B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code>class TestString2 

{

    public static void main(String[] args) 

    {

        String stuName1 = new String(&quot;&#x738B;&#x4E91;&quot;);

        stuName1.concat(&quot;&#x540C;&#x5B66;&quot;);

        System.out.println(stuName1);

    }

}
</code></pre><p>&#x2003;&#x2003;&#x5176;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x662F;&#x201C;&#x738B;&#x4E91;&#x201D;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x201C;&#x738B;&#x4E91;&#x540C;&#x5B66;&#x201D;&#x3002;&#x4E3A;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;</p>
<p>&#x2003;&#x2003;&#x5728;&#x672C;&#x7AE0;&#x5F00;&#x59CB;&#x5C31;&#x4ECB;&#x7ECD;&#x8FC7;String&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5E38;&#x91CF;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x503C;&#x5728;&#x521B;&#x5EFA;&#x4E4B;&#x540E;&#x4E0D;&#x80FD;&#x66F4;&#x6539;&#x3002;concat(String str)&#x65B9;&#x6CD5;&#x7684;&#x8F93;&#x51FA;&#x662F;&#xFF0C;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;String&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x7528;&#x6765;&#x5B58;stuName1&#x5B57;&#x7B26;&#x4E32;&#x52A0;&#x4E0A;&#x201C;&#x540C;&#x5B66;&#x201D;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5728;&#x539F;&#x6765;stuName1&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x540E;&#x9762;&#x589E;&#x52A0;&#x5185;&#x5BB9;&#xFF0C;&#x5BF9;&#x4E8E;stuName1&#x800C;&#x8A00;&#xFF0C;&#x5B83;&#x662F;&#x5E38;&#x91CF;&#xFF0C;&#x5185;&#x5BB9;&#x5E76;&#x6CA1;&#x6709;&#x53D8;&#x5316;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x8F93;&#x51FA;&#x201C;&#x738B;&#x4E91;&#x540C;&#x5B66;&#x201D;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;stuName1.concat(&quot;&#x540C;&#x5B66;&quot;)&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7ED3;&#x679C;&#x8D4B;&#x7ED9;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x8F93;&#x51FA;&#x8BE5;&#x5B57;&#x7B26;&#x4E32;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x2003;&#x2003;&#x518D;&#x770B;&#x4E0B;&#x9762;&#x7684;&#x6848;&#x4F8B;&#x3002;</p>
<pre><code>class TestString3 

{

    public static void main(String[] args) 

    {

        String stuName1 = new String(&quot;&#x738B;&#x4E91;&quot;);

        System.out.println(stuName1);

        stuName1 = &quot;&#x5218;&#x9759;&#x6D9B;&quot;;

        System.out.println(stuName1);

    }

}
</code></pre><p>&#x2003;&#x2003;&#x4EE3;&#x7801;&#x7684;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code>&#x738B;&#x4E91;

&#x5218;&#x9759;&#x6D9B;
</code></pre><p>&#x2003;&#x2003;&#x4E0D;&#x662F;&#x8BF4;String&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x5E38;&#x91CF;&#x5417;&#xFF1F;&#x600E;&#x4E48;&#x4E24;&#x6B21;&#x8F93;&#x51FA;stuName1&#xFF0C;&#x5374;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x4E86;&#x5462;&#xFF1F;&#x7A76;&#x5176;&#x539F;&#x56E0;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x8FD9;&#x91CC;&#x8BF4;&#x7684;&#x4E0D;&#x53EF;&#x53D8;&#x662F;&#x6307;&#x5728;&#x5806;&#x5185;&#x5B58;&#x4E2D;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;String&#x5B57;&#x7B26;&#x4E32;&#x4E0D;&#x53EF;&#x53D8;&#x3002;&#x4E8B;&#x5B9E;&#x4E0A;&#xFF0C;stuName1 = &quot;&#x5218;&#x9759;&#x6D9B;&quot;;&#x8BED;&#x53E5;&#x5DF2;&#x7ECF;&#x65B0;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;String&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x8BA9;stuName1&#x6307;&#x5411;&#x4E86;&#x8FD9;&#x4E2A;&#x65B0;&#x7684;String&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x539F;&#x6765;&#x5B58;&#x653E;&#x201C;&#x738B;&#x4E91;&#x201D;&#x7684;&#x8FD9;&#x4E2A;String&#x5B57;&#x7B26;&#x4E32;&#x6CA1;&#x6709;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x5982;&#x56FE;5.1&#x6240;&#x793A;&#x3002;</p>
<center><img src="https://labfile.oss.aliyuncs.com/library/textbook-java1/img/d5z/tu5.1.png"></center>
<center>&#x56FE;5.1  &#x5F15;&#x7528;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x91CD;&#x8D4B;&#x503C;</center>  



<h3 id="512--string&#x7C7B;&#x7684;&#x4F7F;&#x7528;">5.1.2  String&#x7C7B;&#x7684;&#x4F7F;&#x7528;</h3>
<h4 id="1&#xFF0E;&#x8FDE;&#x63A5;&#x5B57;&#x7B26;&#x4E32;">1&#xFF0E;&#x8FDE;&#x63A5;&#x5B57;&#x7B26;&#x4E32;</h4>
<p>&#x2003;&#x2003;&#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x4E86;&#x91C7;&#x7528;public String concat(String str)&#x65B9;&#x6CD5;&#x8FDE;&#x63A5;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x4E8B;&#x5B9E;&#x4E0A;&#xFF0C;&#x91C7;&#x7528;&#x6700;&#x591A;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528;&#x201C;+&#x201D;&#x8FDB;&#x884C;String&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;&#x3002;&#x4E4B;&#x524D;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x8F93;&#x51FA;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x90FD;&#x662F;&#x4F7F;&#x7528;&#x201C;+&#x201D;&#x8FDB;&#x884C;String&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x8FDE;&#x63A5;&#x3002;</p>
<p>&#x2003;&#x2003;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x5176;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x56FE;5.2&#x6240;&#x793A;&#x3002;</p>
<pre><code>public class TestStringConcat {

    public static void main(String[] args) {

        //&#x4F7F;&#x7528;&quot;+&quot;&#x8FDB;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;

        System.out.println(&quot;&#x4F7F;&#x7528;&apos;+&apos;&#x8FDB;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;&quot;);

        String s1 = &quot;&#x60A8;&#x597D;&quot;;

        s1 = s1 + &quot;,&#x848B;&#x8001;&#x5E08;!&quot;;//&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;&#x7528;&#x6765;&#x8FDE;&#x63A5;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x8BA9;s1&#x6307;&#x5411;&#x8FD9;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;

        System.out.println(s1);

        //&#x4F7F;&#x7528;public String concat(String str)&#x65B9;&#x6CD5;&#x8FDE;&#x63A5;

        System.out.println(&quot;&#x4F7F;&#x7528;public String concat(String str)&#x65B9;&#x6CD5;&#x8FDE;&#x63A5;&quot;);

        String s2 = &quot;&#x60A8;&#x597D;&quot;;

        //&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;&#x7528;&#x6765;&#x8FDE;&#x63A5;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4F46;&#x6CA1;&#x6709;&#x53D8;&#x91CF;&#x6307;&#x5411;&#x8FD9;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;

        s2.concat(&quot;,&#x7530;&#x8001;&#x5E08;!&quot;);

        //&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;&#x7528;&#x6765;&#x8FDE;&#x63A5;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x8BA9;s3&#x6307;&#x5411;&#x8FD9;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;

        String s3 = s2.concat(&quot;,&#x7530;&#x8001;&#x5E08;!&quot;);

        System.out.println(s2);

        System.out.println(s3);

    }

}
</code></pre><center><img src="https://labfile.oss.aliyuncs.com/library/textbook-java1/img/d5z/tu5.2.png"></center>
<center>&#x56FE;5.2  &#x8FDE;&#x63A5;String&#x5B57;&#x7B26;&#x4E32; </center>  






<h4 id="2&#xFF0E;&#x6BD4;&#x8F83;&#x5B57;&#x7B26;&#x4E32;">2&#xFF0E;&#x6BD4;&#x8F83;&#x5B57;&#x7B26;&#x4E32;</h4>
<p>&#x2003;&#x2003;&#x6BD4;&#x8F83;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x7528;&#x7684;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x662F;&#x8FD0;&#x7B97;&#x7B26;&#x201C;==&#x201D;&#x548C;String&#x7C7B;&#x7684;equals&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x2003;&#x2003;&#x4F7F;&#x7528;&#x201C;==&#x201D;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x662F;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5730;&#x5740;&#x662F;&#x5426;&#x4E00;&#x81F4;&#xFF0C;&#x672C;&#x8D28;&#x4E0A;&#x5C31;&#x662F;&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5426;&#x6307;&#x5411;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5219;&#x8FD4;&#x56DE;true&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE;false&#x3002;&#x800C;String&#x7C7B;&#x7684;equals&#x65B9;&#x6CD5;&#x5219;&#x662F;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;String&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5185;&#x5BB9;&#x662F;&#x5426;&#x4E00;&#x81F4;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x2003;&#x2003;&#x5148;&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code>public class TestStringEquals {

    public static void main(String[] args) {

        String s1 = &quot;Java&#x57FA;&#x7840;&quot;;

        String s2 = &quot;Java&#x57FA;&#x7840;&quot;;

        System.out.println(s1 == s2);                           //&#x8FD4;&#x56DE;true

        System.out.println(s1.equals(s2));           //&#x8FD4;&#x56DE;true

        String s3 = new String(&quot;&#x524D;&#x7AEF;&#x6280;&#x672F;&quot;);

        String s4 = new String(&quot;&#x524D;&#x7AEF;&#x6280;&#x672F;&quot;);

        System.out.println(s3 == s4);                           //&#x8FD4;&#x56DE;false

        System.out.println(s3.equals(s4));           //&#x8FD4;&#x56DE;true

    }

}
</code></pre><p>&#x2003;&#x2003;&#x7A0B;&#x5E8F;&#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x56FE;5.3&#x6240;&#x793A;&#x3002;</p>
 <center><img src="https://labfile.oss.aliyuncs.com/library/textbook-java1/img/d5z/tu5.3.png"></center>
<center> &#x56FE;5.3  &#x6BD4;&#x8F83;String&#x5B57;&#x7B26;&#x4E32; </center>  



                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="d5z.html" class="navigation navigation-prev " aria-label="Previous page: 第五章    String字符串">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="5.2String类的常用方法.html" class="navigation navigation-next " aria-label="Next page: 5.2  String类的常用方法">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"5.1  String类","level":"1.6.1","depth":2,"next":{"title":"5.2  String类的常用方法","level":"1.6.2","depth":2,"path":"chapters/d5z/5.2String类的常用方法.md","ref":"chapters/d5z/5.2String类的常用方法.md","articles":[]},"previous":{"title":"第五章    String字符串","level":"1.6","depth":1,"path":"chapters/d5z/d5z.md","ref":"chapters/d5z/d5z.md","articles":[{"title":"5.1  String类","level":"1.6.1","depth":2,"path":"chapters/d5z/5.1String类.md","ref":"chapters/d5z/5.1String类.md","articles":[]},{"title":"5.2  String类的常用方法","level":"1.6.2","depth":2,"path":"chapters/d5z/5.2String类的常用方法.md","ref":"chapters/d5z/5.2String类的常用方法.md","articles":[]},{"title":"5.3  Java API文档的使用","level":"1.6.3","depth":2,"path":"chapters/d5z/5.3JavaAPI文档的使用.md","ref":"chapters/d5z/5.3JavaAPI文档的使用.md","articles":[]},{"title":"5.4  StringBuffer类","level":"1.6.4","depth":2,"path":"chapters/d5z/5.4StringBuffer类.md","ref":"chapters/d5z/5.4StringBuffer类.md","articles":[]},{"title":"5.5  本章练习","level":"1.6.5","depth":2,"path":"chapters/d5z/5.5本章练习.md","ref":"chapters/d5z/5.5本章练习.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapters/d5z/5.1String类.md","mtime":"2020-04-22T12:28:13.912Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-22T12:09:46.327Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

